<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */

namespace app\api\controller;


use app\common\model\StudentHabit;
use app\common\model\StudentLog;

class Student extends Base
{
    public $studentModel;

    public function __construct()
    {
        parent::__construct();
        $this->studentModel = new \app\common\model\Student();
    }

    /**
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function student()
    {
        if (request()->isPost()) {
//            $data = input('');
//            $where = [
//                'id' => $data['sid']
//            ];
//
//
//
//            $student = $this->studentModel->where($where)->find();
//
//            if ($student) {
//                $student = $student->toArray();
//                $student['images'] = json_decode($student['images'], true);
//            }


            return returnJson(200, '', $this->student);
        }
        return returnJson(202, '暂无记录');
    }

    public function basic()
    {
        if (request()->isPost()) {
            $data = input('');
            if(!$data['sid']){
				return returnJson(202,'学员信息不存在');
			}
            $info = $this->studentModel->where(['id' => $data['sid']])->find()->toArray();
            $info['images'] = json_decode($info['images'], true);
            return returnJson(200, '', $info);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function habit()
    {
        if (request()->isPost()) {
            $data = input('');
            if (isset($data['type']) && $data['sid']) {
                if ($data['type']) {
                    $type_name = [
                        2 => '请假事宜',
                        3 => '比赛记录',
                        4 => '课堂反馈'
                    ];
                    $res = StudentHabit::getAll(['type' => $data['type'], 'sid' => $data['sid']], 'time desc');
                    if($res){
                        foreach ($res as $key => $val){
                            $res[$key]['type_name'] = $type_name[$val['type']];
                            $res[$key]['type'] = $data['type'];
                        }
                    }

                } else {
                    $log = StudentLog::getAll(['sid' => $data['sid']], 'time asc');
                    $res = [];
                    if ($log) {
                        foreach ($log as $value) {
                            if (!is_null($value['height'])) {
                                $res['height'][0][] = $value['time'];
                                $res['height'][1][] = $value['height'];
                            }
                            if (!is_null($value['width'])) {
                                $res['width'][0][] = $value['time'];
                                $res['width'][1][] = $value['width'];
                            }
                            if (!is_null($value['lung_capacity'])) {
                                $res['lung_capacity'][0][] = $value['time'];
                                $res['lung_capacity'][1][] = $value['lung_capacity'];
                            }
                            if (!is_null($value['sprint'])) {
                                $res['sprint'][0][] = $value['time'];
                                $res['sprint'][1][] = $value['sprint'];
                            }
                            if (!is_null($value['jump'])) {
                                $res['jump'][0][] = $value['time'];
                                $res['jump'][1][] = $value['jump'];
                            }
                            if (!is_null($value['throwing'])) {
                                $res['throwing'][0][] = $value['time'];
                                $res['throwing'][1][] = $value['throwing'];
                            }
                            if (!is_null($value['long_sprint'])) {
                                $res['long_sprint'][0][] = $value['time'];
                                $res['long_sprint'][1][] = $value['long_sprint'];
                            }
                            if (!is_null($value['sit_and_reach'])) {
                                $res['sit_and_reach'][0][] = $value['time'];
                                $res['sit_and_reach'][1][] = $value['sit_and_reach'];
                            }
                        }
                    }
                    !isset($res['height']) && $res['height'][] = [] && $res['height'][] = [];
                    !isset($res['width']) && $res['width'][] = [] && $res['width'][] = [];
                    !isset($res['lung_capacity']) && $res['lung_capacity'][] = [] && $res['lung_capacity'][] = [];
                    !isset($res['sprint']) && $res['sprint'][] = [] && $res['sprint'][] = [];
                    !isset($res['jump']) && $res['jump'][] = [] && $res['jump'][] = [];
                    !isset($res['throwing']) && $res['throwing'][] = [] && $res['throwing'][] = [];
                    !isset($res['long_sprint']) && $res['long_sprint'][] = [] && $res['long_sprint'][] = [];
                    !isset($res['sit_and_reach']) && $res['sit_and_reach'][] = [] && $res['sit_and_reach'][] = [];
                }
                return returnJson(200, '', $res ? $res : []);
            } else {
                return returnJson(200, '', []);
            }
        }
    }

    public function add_habit()
    {
        if (request()->isPost()) {
            $data = input('');
            if (!$data['sid']) {
                return returnJson(202, '队员不存在');
            }
            if (!$data['type']) {
                return returnJson(202, '请选择记录类型');
            }
            if (!$data['content']) {
                return returnJson(202, '请填写记录类型');
            }
            $save_data = [
                'time' => $data['time'] ? $data['time'] : date('Y-m-d'),
                'content' => $data['content'],
                'create_time' => time(),
                'update_time' => time(),
                'sid' => $data['sid'],
                'type' => $data['type'],
                'end_time' => $data['endtime'] ? $data['endtime'] : ''
            ];
            $res = StudentHabit::create($save_data);
            if ($res) {
                return returnJson(200, '记录成功');
            }
        }
        return returnJson(202, '记录失败');
    }

}